package mse.eai.jbpm.registerBetProcess;

import java.util.Map;

import org.jboss.logging.Logger;
import org.jbpm.graph.exe.ExecutionContext;
import org.jbpm.graph.node.DecisionHandler;

public class IsMovementValid implements DecisionHandler {

	private static final long serialVersionUID = -3442949152917984468L;
	private static final Logger log = Logger.getLogger(IsMovementValid.class);

	// must return YES or NO
	public String decide(ExecutionContext context) throws Exception {

		log.info("Executing ---------->");

		String transition = null;

		try {

			Map<String, Object> fromESB = (Map<String, Object>) context
					.getVariable("fromESB");
			log.info("Got from context: " + "fromESB  = " + fromESB);
			log.info (fromESB.get("checkOperationResponse.return")
					.getClass());
			log.info(fromESB.get("checkOperationResponse.return").getClass());

			String response = (String) fromESB
					.get("checkOperationResponse.return");

			if (response.equals("true")) {
				log.info("BANK MOVEMENT ALLOWED");
				transition = "YES";
			} else {
				log.info("BANK MOVEMENT NOT ALLOWED");
				transition = "NO";
			}
			log.info("Transition chosen : " + transition);
			
		} catch (Exception ex) {
			log.error("Exception: ", ex);
			log.error("ProcessInstance will be suspended");
			context.getProcessInstance().suspend();
		}

		log.info("<--------------- Done");
		
		return transition;
	}
}